<template>
  <div class="login">
    <div class="login-flex">
      <div class="login-logo">
        <h1>&nbsp;</h1>
      </div>
  
      <div class="login-form">
        <div class="login-input vux-1px-b">
          <label for="user"
                 class="icon iconfont icon-account"></label>
          <input id="user"
                 v-model="user"
                 placeholder="用户名"
                 style="width:80%"
                 required></input>
        </div>
  
        <div class="login-input vux-1px-b">
          <label for="pass"
                 class="icon iconfont icon-security"></label>
          <input id="pass"
                 v-model="pass"
                 type="password"
                 placeholder="密码"
                 style="width:80%"
                 required></input>
        </div>
  
        <div class="login-submit">
          <button type="button"
                  @click="loginSubmit">登录</button>
        </div>
      </div>
  
      <div class='footer'>
        <p>说明:</p>
        <p>
          登陆账号为您在我司的外网注册的账号和密码；
        </p>
        <p>
          我司的外网地址：http://pur.chngalaxy.com/；
        </p>
        <!-- <p>
          如遗忘账号和密码，请及时***，电话：**********
        </p> -->
      </div>
      <!--<div class="login-links">
            <a href=""
               class="vux-1px-r">注册账号</a>
            <a href="">忘记密码</a>
          </div>-->
    </div>
  </div>
</template>

<script>
import { Loading, Toast, md5, querystring } from 'vux'
import { LoginService } from '../service/Services.js'
import config from '../utils/config'

export default {
  name: 'Login',
  components: {
    Loading,
    Toast
  },
  data () {
    return {
      user: '',
      pass: '',
      code: ''
    }
  },
  methods: {
    loginSubmit () {
      if (this.user === '' || this.pass === '') {
        this.$vux.toast.show({
          text: '用户名和密码不能为空',
          type: 'cancel',
          width: '180px'
        })
        return
      }
      this.$vux.loading.show({
        text: '登录中...'
      })

      LoginService.login(this.user, md5(this.pass), this.code).then((res) => {
        this.$vux.loading.hide()
        if (!res.Result) {
          this.$vux.toast.show({
            text: res.ErrorMessage,
            type: 'cancel',
            width: '180px'
          })
        } else {
          // 写入UserInfo的值
          this.$store.dispatch('setUserInfo', res.Data[0])
          // this.$store.commit('setUserInfo', res.Data[0])
          // 跳转页面
          this.$router.push({path: '/demos/index'})
        }
      }).catch((res) => {
        this.$vux.loading.hide()
        this.$vux.toast.show({
          text: '登录失败',
          type: 'cancel',
          width: '180px'
        })
      })
    },
    loadWxLogin () {
      // 获取Code
      var reg = /code=\w+/
      let code = window.location.href.match(reg)
      if (code) {
        this.code = code[0].replace('code=', '')
      }
      if (process.env.NODE_ENV === 'production') {
        if (!this.code) {
          if (!this.$store.getters.getUserInfo) {
            let wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?'
            let data = {
              appid: config.APPID,
              redirect_uri: config.WEBSITE + '#/demos/xh-login',
              response_type: 'code',
              scope: 'snsapi_userinfo',
              state: '1#wechat_redirect'
            }
            window.location = wxUrl + querystring.stringify(data)
          }
        }
      }
    }
  },
  created () {
    this.loadWxLogin()
  }
}
</script>

<style scoped>
.login {
  box-sizing: border-box;
  background-color: #fff;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  background-image: url(../assets/img/login-bg.png);
  background-size: 100% 100%;
}

.login-flex {
  width: 100%;
}

.login-logo {
  text-align: center;
  margin-bottom: 80px;
  margin-top: 50px;
  font-size: 16px;
  background: url(../assets/img/xh-logo.png) no-repeat;
  background-size: contain;
  background-position: center;
}

.login-form {
  padding: 0 50px;
}

.login-input {
  box-sizing: border-box;
  /*width: 275px;*/
  margin: 25px auto 0;
  padding: 5px 10px;
}

.login-input input {
  background-color: inherit;
  border: 0;
  font-size: 14px;
}

.login-input .icon {
  display: inline-block;
  min-width: 17px;
  font-size: 17px;
  margin-right: 18px;
  color: #B2B2B2;
}

.login-submit {
  margin-top: 30px;
  text-align: center;
}

.login-submit button {
  width: 100%;
  background-color: #D93333;
  color: #fff;
  line-height: 44px;
  font-size: 16px;
  border: 0;
  border-radius: 44px;
}

.login-links {
  text-align: center;
  margin-top: 20px;
  font-size: 0;
}

.login-links a {
  font-size: 12px;
  padding: 0 10px;
  color: #0076FF
}

.footer {
  margin: 20px 30px;
  font-family: PingFangSC-Regular;
  font-size: 12px;
  color: #666666;
  letter-spacing: 0;
  line-height: 21px;
}
</style>
